<?php
    //require section
    require_once dirname(__FILE__) . '/shared/config/config.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_document.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_document_manage.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_redirect.php';
    $title = 'Chi tiết bài tập';

    $doc_id = 0;
    if (!empty($_GET['doc_id'])) {
        $doc_id = $_GET['doc_id'];
    } else {
        lib_redirect::Redirect('/document_manage.php');
    }
    $document = api_document::get_document_by_id($doc_id);
?>

<!DOCTYPE html>
<html>
<head>
    <?php
        require_once DOCUMENT_ROOT . DIR_INCLUDE . 'head.php';
    ?>
    <link href="<?php echo LINK_ROOT . DIR_SHARED_LAYOUT_CSS . 'lightbox.css' ?>" rel='stylesheet'>
</head>
<body>
<!--script to close opened image by keypress-->
<script type="text/javascript">
    $(document).keypress(function () {
        if (window.location.href.indexOf("img-box") > -1) {
            window.location = '#main-content';
        }
    });
</script>

<div class="uk-container uk-container-center uk-margin-top">
    <?php
        require_once DOCUMENT_ROOT . DIR_INCLUDE . 'menu.php';
        require_once DOCUMENT_ROOT . DIR_INCLUDE . 'slider.php';

        $user = $_SESSION['user_info'];
        $buy_status = api_document_manage::check_if_doc_paid($user['user_id'], $document['doc_id']);
    ?>

    <section id='main-content' class="ts-body-container uk-panel uk-panel-box ts-padding-large">

        <?php
            if (empty($document)) {
                echo "<h2>Tài liệu không tìm thấy.</h2>";
            } else {
                $doc_img = "doc.png";
                if (!empty($document['doc_img_url'])) {
                    $doc_img = $document['doc_img_url'];
                }
                ?>
                <h2>Chi tiết tài liệu</h2>
                <?php
                if (!empty($success_info)) {
                    ?>
                    <h3>
                        <div class="success"><?php echo $success_info; ?></div>
                    </h3>
                <?php
                } ?>
                <div class="ts-detail-wrapper">
                    <div class="ts-img-left">
                        <!--image in document image folder-->
                        <a class="lightbox" href="#img-box">
                            <img src="<?php echo LINK_ROOT . DIR_SHARED_UPLOAD_IMAGES_DOCUMENT . $doc_img; ?>"/>
                        </a>

                        <div class="lightbox-target" id="img-box" onclick="window.location = '#main-content'">
                            <img src="<?php echo LINK_ROOT . DIR_SHARED_UPLOAD_IMAGES_DOCUMENT . $doc_img; ?>"/>
                            <a class="lightbox-close" href="#main-content"></a>
                        </div>

                        <div class="uk-text-center"><i><?php echo $document['doc_name']; ?></i></div>
                    </div>
                    <div class="ts-content-right">
                        <table>
                            <tr>
                                <th>Mã bài tập:</th>
                                <td><?php echo $document['doc_id']; ?></td>
                            </tr>
                            <tr>
                                <th>Tên bài tập:</th>
                                <td><?php echo $document['doc_name']; ?></td>
                            </tr>
                            <tr>
                                <th>Mô tả:</th>
                                <td>
                                    <?php echo $document['doc_detail']; ?>
                                </td>
                            </tr>
                            <tr>
                                <th>Thuộc danh mục:</th>
                                <td><?php echo $document['doc_cat_name']; ?></td>
                            </tr>
                            <tr>
                                <th>Giá:</th>
                                <td>
                                    <b style="color: #d35400;"><?php echo number_format($document['doc_price'], 0, '.', ','); ?>
                                        vnđ</b></td>
                            </tr>
                            <tr>
                                <th>Ngày tạo:</th>
                                <?php
                                $date = date_create($document["doc_added_date"]);
                                $date_str = date_format($date, "d/m/Y H:i");
                                ?>
                                <td><?php echo $date_str; ?></td>
                            </tr>
                        </table>
                    </div>
                </div>
                <form action="?doc_id=<?php echo $document['doc_id']; ?>" name="form_add_to_card" method="POST">
                    <?php if($buy_status) : ?>
                    <div class="uk-text-center uk-margin-bottom">
                        Để tải tài liệu, bạn hãy bấm nút <b>"Tải về"</b> hoặc bấm <b>chuột phải</b> vào nút <b>"Tải về"</b> chọn <b>"Save Link As..."</b>
                    </div>
                    <?php endif; ?>
                    <div class="uk-text-center">
                        <a href="document_manage.php" class='uk-button'>Quay lại</a>
                        <?php if ($buy_status) : ?>
                            <a href="<?php echo LINK_ROOT . DIR_SHARED_UPLOAD_DOCUMENTS . $document['doc_file']; ?>" class="uk-button uk-button-primary">Tải về</a>
                        <?php endif; ?>
                    </div>
                </form>
            <?php
            }
        ?>
    </section>

    <?php
        require_once DOCUMENT_ROOT . DIR_INCLUDE . 'footer.php';
    ?>
</div>
</body>
</html>